import { Controller } from "egg";
import validateInput from "../decorator/inputValidate";
import { testCreateRules } from "../validatorRules/test";

export default class TestController extends Controller {
  public async index() {
    const { ctx } = this;
    ctx.body = await ctx.service.test.findList();
  }

  @validateInput(testCreateRules, "createTestAlreadyExists")
  public async create() {
    const { ctx, service } = this;
    const { name } = ctx.request.body;
    const testName = await service.test.findByUsername(name);
    if (testName) {
      return ctx.helper.error({ ctx, errorType: "createTestAlreadyExists" });
    }
    const testData = await service.test.add(ctx.request.body);
    ctx.helper.success({ ctx, res: testData });
  }
}
